from Utils import *
import time
import os
from logic.PathConfig import *
import Config


# 返回值一定要是字典
def GetAllFilePath(args):
    data = JsonStr2dict(args)["path"]
    path = data

    file_paths = []
    for root, dirs, files in os.walk(path):
        for file in files:
            file_path = os.path.join(root, file)
            file_paths.append(file_path)
    data = {"filePaths" : file_paths}
    return data

def GetAllFilePaths(path):
    file_paths = []
    for root, dirs, files in os.walk(path):
        for file in files:
            file_path = os.path.join(root, file)
            file_paths.append(file_path)
    return file_paths


# 遍历读取指定路径下所有version.json文件["platform"]
def GetAllVerinfo(args):
    data = JsonStr2dict(args)
    print(data["platform"])
    platform = data["platform"]

    absPath = GetAbsPath()
    absPath = absPath + "/../../" + updateFilePath

    absupdateFilePath = os.path.join(absPath, platform)  
    filesList = []
    files = GetAllFilePaths(absupdateFilePath)
    for file in files:
        fileName = os.path.basename(file)
        if fileName == versionFileName :
            print(file)
            filesList.append(file)

    verInfoList = []
    for file in filesList:
        with open(file, 'r', encoding='utf-8') as f:
            jdic = json.load(f) 
            verInfoList.append(jdic)

    for data in verInfoList:
        print("处理结果==》" + str(data))

    VerInfos = {"AllVerInfo" : verInfoList}
    return VerInfos

def GetABInfo(args):
    data = JsonStr2dict(args)
    absPath = GetAbsPath()
    abInfoPath = absPath + "/../../" + updateFilePath + "/" + data["platform"] + "/" + data["version"] + "/" + ABInfoFilePath

    if not os.path.exists(abInfoPath):
        print("json路径不存在:" + abInfoPath )
        return None

    with open(abInfoPath, 'r', encoding='utf-8') as f:
        jdic = json.load(f) 
    
    print("处理结果==》" + str(jdic)  )      

    return jdic

# 直接返回ftp服务器的地址和用户通行证
def GetFTPInfo(args):
    ipAndPort = "ftp://" + Config.curIP + ":" + str(Config.ftpPort)

    jdic = {
        "url" : ipAndPort,
        "ftpUserID" : Config.username,
        "ftpPassword" : Config.password,
    }

    return jdic